import { setItem } from '@/utils/storage'
import { defineStore } from 'pinia'

export const pickStore = defineStore('pickStore', {
  state: () => {
    return {
      pickClientIp: localStorage.getItem('pickClienttIp') || '127.0.0.1',
      pickClientPort: parseInt(localStorage.getItem('pickClientPort')) || null,
      pickServerPort: parseInt(localStorage.getItem('pickServerPort')) || null,
      pickClientStatus: localStorage.getItem('pickClientStatus') === 'true',
      pickServerStatus: localStorage.getItem('pickServerStatus') === 'true',
      pickLog: JSON.parse(sessionStorage.getItem('pickLog')) as Array<String> || [] as Array<String>
    }
  },
  actions: {
    setPickLog (payLoad:string) {
      if (this.pickLog.length >= 200) {
        this.pickLog.shift()
      }
      this.pickLog.push(payLoad)
      sessionStorage.setItem('pickLog', JSON.stringify(this.pickLog))
    },
    setPickClientStatus (payLoad:boolean) {
      this.pickClientStatus = payLoad
      setItem('pickClientStatus', JSON.stringify(payLoad))
    },
    setPickServerStatus (payLoad:boolean) {
      this.pickServerStatus = payLoad
      setItem('pickServerStatus', JSON.stringify(payLoad))
    },
    setPickClientIp (payLoad:string) {
      this.pickClienttIp = payLoad
      setItem('pickClienttIp', payLoad)
    },
    setPickClientPort (payLoad:number) {
      this.pickClientPort = payLoad
      setItem('pickClientPort', JSON.stringify(payLoad))
    },
    setPickServerPort (payLoad:number) {
      this.pickServerPort = payLoad
      setItem('pickServerPort', JSON.stringify(payLoad))
    }
  }
})
